/********************************************************************/
/* Copyright (C) School of Software Engineering of USTC, 2012                                */
/*                                                                                                                       */
/*  FILE NAME: dbapi.c                                                                                          */
/*  PRINCIPAL AUTHOR: SA12226114(Xin Zhu), SA12226139(Dan Shu)                    */
/*  SUBSYSTEM NAME: DB                                                                                    */
/*  MODULE NAME: dbapi                                                                                      */
/*  LANGUAGE: C                                                                                                 */
/*  TARGET ENVIRONMENT: Linux                                                                          */
/*  DATE OF FIRST RELEASE: 2012-11-27                                                             */
/*  DESCRIPTION: Impement of Abstract Interface for Tokyo Cabinet DB API              */
/********************************************************************/

/* include system header file */
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>

/* include private header file */
#include "tcutil.h"
#include "tchdb.h"
#include "dbapi.h"


/*****************************************************************
Function Name: db_new
Description: implement a unified database initialize interface for diverse db
Parameter:  void
Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
Comment: We don't need to check input parameters, because the corresponding 
                interface of TC has checked input parameters.
******************************************************************/
void* db_new(void)
{
    return tchdbnew();
}


/*****************************************************************
Function Name: db_open
Description: implement a unified database open interface for diverse db
Parameter:  @db: a pointer to database object
Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
Comment: We don't need to check input parameters, because the corresponding 
                interface of TC has checked input parameters.
******************************************************************/
void db_open(void *db)
{
    int ecode;

    /* invoke open interface of TC to open a database */
    if(!tchdbopen(db, "casket.tch", HDBOWRITER | HDBOCREAT))
    {
        ecode = tchdbecode(db);
        fprintf(stderr, "open error: %s\n", tchdberrmsg(ecode));
        printf("tchdbnew() failed!\n");
    }

    return;
}


/*****************************************************************
Function Name: db_write
Description: implement a unified database write interface for diverse db
Parameter:  
@db: a pointer to database object
@key: a pointer to key 
@value: a pointer to value

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
Comment: We don't need to check input parameters, because the corresponding 
                interface of TC has checked input parameters.
******************************************************************/
void db_write(void *db, char *key, char *value)
{
    int ecode;

    /* invoke put interface of TC to write pairs of key and value into database */
    if(!tchdbput2(db, key, value))
    {
        ecode = tchdbecode(db);
        fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
    }

    return;
}


/*****************************************************************
Function Name: db_read
Description: implement a unified database read interface for diverse db
Parameter:  
@db: a pointer to database object
@key: a pointer to key 

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
Comment: We don't need to check input parameters, because the corresponding 
                interface of TC has checked input parameters.
******************************************************************/
void db_read(void *db, char *key)
{
    char *value;
    int ecode;

    /* invoke get interface of TC to read value from database in according to key */
    value = tchdbget2(db, key);
    if(value)
    {
        printf("%s\n", value);
        free(value);
    } 
    else
    {
        ecode = tchdbecode(db);
        fprintf(stderr, "get error: %s\n", tchdberrmsg(ecode));
    }

    return;
}


/*****************************************************************
Function Name: db_close
Description: implement a unified database close interface for diverse db
Parameter: @db: a pointer to database object
Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
Comment: We don't need to check input parameters, because the corresponding 
                interface of TC has checked input parameters.
******************************************************************/
void db_close(void *db)
{
    int ecode;

    /* invoke close interface of TC to close database  */
    if(!tchdbclose(db))
    {
        ecode = tchdbecode(db);
        fprintf(stderr, "close error: %s\n", tchdberrmsg(ecode));
    }

    return;
}


/*****************************************************************
Function Name: db_delete
Description: implement a unified database delete interface for diverse db
Parameter: @db: a pointer to database object
Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
Comment: We don't need to check input parameters, because the corresponding 
                interface of TC has checked input parameters.
******************************************************************/
void db_delete(void *db)
{
    /* invoke delete interface of TC to delete database object  */
    tchdbdel(db);

    return;
}


